package com.children.growth.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.children.growth.common.Result;
import com.children.growth.entity.MentalHealth;
import com.children.growth.service.MentalHealthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 心理健康控制器
 * 
 * @author children
 * @since 1.0.0
 */
 @RestController
 @RequestMapping("/mental-health")
public class MentalHealthController {

    @Autowired
    private MentalHealthService mentalHealthService;

    /**
     * 创建心理健康记录
     */
    @PostMapping("/create")
    public Result<Boolean> createMentalHealth(@RequestBody MentalHealth mentalHealth) {
        boolean success = mentalHealthService.createMentalHealth(mentalHealth);
        return success ? Result.success(true) : Result.error("创建心理健康记录失败");
    }

    /**
     * 获取心理健康记录列表
     */
    @GetMapping("/list")
    public Result<Page<MentalHealth>> getMentalHealthList(@RequestParam("userId") Long userId,
                                                          @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                          @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                                          @RequestParam(value = "warningLevel", required = false) String warningLevel,
                                                          @RequestParam(value = "startDate", required = false) String startDate,
                                                          @RequestParam(value = "endDate", required = false) String endDate) {
        Page<MentalHealth> page = mentalHealthService.getMentalHealthList(userId, pageNum, pageSize, warningLevel, startDate, endDate);
        return Result.success(page);
    }

    /**
     * 获取心理健康记录详情
     */
    @GetMapping("/{id}")
    public Result<MentalHealth> getMentalHealthById(@PathVariable Long id) {
        MentalHealth mentalHealth = mentalHealthService.getMentalHealthById(id);
        return mentalHealth != null ? Result.success(mentalHealth) : Result.error("心理健康记录不存在");
    }

    /**
     * 更新心理健康记录
     */
    @PutMapping("/update")
    public Result<Boolean> updateMentalHealth(@RequestBody MentalHealth mentalHealth) {
        boolean success = mentalHealthService.updateMentalHealth(mentalHealth);
        return success ? Result.success(true) : Result.error("更新心理健康记录失败");
    }

    /**
     * 删除心理健康记录
     */
    @DeleteMapping("/{id}")
    public Result<Boolean> deleteMentalHealth(@PathVariable Long id) {
        boolean success = mentalHealthService.deleteMentalHealth(id);
        return success ? Result.success(true) : Result.error("删除心理健康记录失败");
    }

    /**
     * 进行心理健康评估
     */
    @PostMapping("/assess/{userId}")
    public Result<Map<String, Object>> assessMentalHealth(@PathVariable Long userId,
                                                          @RequestBody Map<String, Object> answers) {
        Map<String, Object> result = mentalHealthService.assessMentalHealth(userId, answers);
        return Result.success(result);
    }

    /**
     * 获取心理健康统计
     */
    @GetMapping("/statistics/{userId}")
    public Result<Map<String, Object>> getMentalHealthStatistics(@PathVariable Long userId) {
        Map<String, Object> statistics = mentalHealthService.getMentalHealthStatistics(userId);
        return Result.success(statistics);
    }

    /**
     * 获取心理健康建议
     */
    @GetMapping("/advice")
    public Result<String> getMentalHealthAdvice(@RequestParam("assessmentType") Integer assessmentType,
                                               @RequestParam("score") Integer score) {
        String advice = mentalHealthService.getMentalHealthAdvice(assessmentType, score);
        return Result.success(advice);
    }

    /**
     * 获取情绪趋势数据
     */
    @GetMapping("/trend")
    public Result<List<Map<String, Object>>> getEmotionTrend(@RequestParam("userId") Long userId,
                                                             @RequestParam(value = "days", defaultValue = "30") Integer days) {
        List<Map<String, Object>> trendData = mentalHealthService.getEmotionTrend(userId, days);
        return Result.success(trendData);
    }

    /**
     * 确认评估结果
     */
    @PutMapping("/confirm/{id}")
    public Result<Boolean> confirmAssessment(@PathVariable Long id) {
        boolean success = mentalHealthService.confirmAssessment(id);
        return success ? Result.success(true) : Result.error("确认评估失败");
    }
} 